﻿using UnityEngine;
using System.Collections;

public class PauseGame : MonoBehaviour
{

    private bool pause;
    private int boxWidth, boxHeight, boxLeft, boxUp;
    private int buttonLeft, buttonUp, buttonWidth, buttonHeight;

    private GameObject directionalLight;

    void Awake()
    {
        boxWidth = Screen.width / 4;
        boxHeight = Screen.height / 2;
        boxLeft = (Screen.width / 2) - (boxWidth / 2);
        boxUp = (Screen.height / 2) - (boxHeight / 2);

        buttonWidth = (boxWidth / 4) * 3;
        buttonHeight = boxHeight / 3;
        buttonLeft = (boxLeft + (buttonWidth / 5));
        buttonUp = boxUp + ((buttonHeight / 4) * 3);

        directionalLight = GameObject.FindGameObjectWithTag("DirectionalLight");
    }

    void LateUpdate()
    {

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            pause = !pause;

            Pause();
        }
    }

    void OnGUI()
    {
        if (pause)
        {
            GUI.Box(new Rect(boxLeft, boxUp, boxWidth, boxHeight), "JOGO PAUSADO");

            if (GUI.Button(new Rect(buttonLeft, buttonUp, buttonWidth, buttonHeight), "CONTINUAR"))
            {
                pause = false;
                Pause();
            }

            if (GUI.Button(new Rect(buttonLeft, buttonUp + buttonHeight + (buttonHeight / 5), buttonWidth, buttonHeight), "SAIR"))
            {
                pause = false;
                Pause();

                Game_Load.getInstance().LoadGame();
                Game_Save.getInstance().SaveGame();
                Game_Load.getInstance().LoadGame();

                Application.LoadLevel(0);
            }
        }
    }

    private void Pause()
    {
        if (pause)
        {
            Time.timeScale = 0;

            //Deixar o jogo escuro
            directionalLight.GetComponent<Light>().color = Color.black;
        }
        else
        {
            Time.timeScale = 1;

            //Deixar o jogo claro novamente.
            directionalLight.GetComponent<Light>().color = Color.white;
        }
    }
}
